/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bakenote.core;

/**
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
**/

/**
 *
 * @author lello
 */

public class Ingredient {

	// Basic ingredientName class
	private String ingredientName = "";
	private double percent;
    private double exactPercent;
	private Amount amount = Amount.valueOf("");
	private String unit = "g";
	private boolean  isReference = false;

	//private static NumberFormat nf = NumberFormat.getNumberInstance(Locale.ITALIAN);

	public Ingredient() { /* Create empty ingredient */}

	public Ingredient(String s, String w, String u) {
		ingredientName = s;
		amount = Amount.valueOf(w);
		unit = u;
	}

	public Ingredient(String s, Amount w, String u) {
		ingredientName = s;
		amount = w;
		unit = u;
	}

	public Ingredient(String s, Amount w, double x) {
		ingredientName = s;
		amount = w;
		percent = x;
        exactPercent = x;
	}

	public Ingredient(String s, Amount w) {
		ingredientName = s;
		amount = w;
	}

	public Ingredient(String s) {
		ingredientName = s;
	}

	/**
	 *
	 * @param ing
	 * Copy constructor
	 */
	public Ingredient(Ingredient ing) {
		ingredientName = ing.getName();
		amount = ing.getAmount();
		percent = ing.getPercent();
        exactPercent = ing.getPercent();
		unit = ing.getUnit();
		isReference = ing.isReference();
	}

	public void setIngredient(Ingredient ing) {
		setName(ing.getName());
		setAmount(ing.getAmount());
		setPercent(ing.getPercent());
		setUnit(ing.getUnit());
		setReference(ing.isReference());
	}

	public void setName(String s) {
		ingredientName = s;
	}

	public void setPercent(double f) {
		percent = f;
	}

    public void setExactPercent(double f) {
        exactPercent = f;
    }

	public void setAmount(String s) {
		amount = Amount.valueOf(s);
		/**try {
			double w = nf.parse(s).doubleValue();
			amount = Amount.valueOf(w);
		} catch (ParseException ex) {
			System.out.println("assign amount string"+s);
			amount = Amount.valueOf(s);
		}**/
	}

	public void setAmount(double w) {
		//System.out.println("assign amount double "+w);
		amount = Amount.valueOf(w);
	}

	public void setAmount(Amount a) {
		//System.out.println("assign amount Amount "+a);
		amount = a;
	}

	public void setUnit(String u) {
		unit = u;
	}

	public void setReference(boolean b) {
		isReference = b;
	}

	public String getName() {
		return ingredientName;
	}

	public String getIngredientName() {
		return ingredientName;
	}

	public double getPercent() {
		return percent;
	}

    /**
     * exactPercet is the value actually used in calculations.
     * The variable percent is used only for display pourposes.
     * this helps to avoid the low accuracy which is caused by a number with
     * only two digits as displayed in the FotmulaTableView.
     * @return
     */
    public double getExactPercent() {
		return exactPercent;
	}

	public Amount getAmount() {
		return amount;
	}

	/*
	 * This method can be used to calculate the total amount
	 * of a list of ingredients.
	 */
	public double getMeasurableAmount() {
		return amount.isMeasurable() ? amount.getDouble() : 0.0d ;
	}

	public String getUnit() {
		return unit;
	}

	public boolean isReference() {
		return isReference;
	}

	public boolean contains(String s) {
		// not yet implemented
		return ingredientName.toLowerCase().contains(s.toLowerCase());
	}

}
